Skip to content

emlog结构解析 MVC的显示方式及模板的调度过程

字数
898 字
阅读时间
5 分钟
更新日期
8/8/2016

‍‍版权声明:本文系小草窝x.hacking8.com原创尊重作者劳动,转载请标明出处。本文转载自x.hacking8.com谢谢‍‍

最近在看emlog的源码,觉得写得不错的地方就记录下来。作为一名phper的新手,觉得里面有很多地方值得借(chao)鉴(xi)的。另外最近想修改emlog的内核使之可以兼容WordPress的模板(想法),所以想研究下emlog。

首先要学习的是emlog MVC设计模式,(听说emlog没有用框架是自己写的),MVC即使model view Controller的缩写,是模型(model)-视图(view)-控制器(controller)的缩写

但严格来说,emlog只有MC没有V,emlog的view类似于WordPress的,模板都是由.php组成,里面调用内置的函数。

Emlog加载时调用了**_autoload** 这个函数的功能是自动寻找调用的Class

所以所有的类都在include这个目录下

首页index.php调用的

php
define('TEMPLATE_PATH', TPLS_PATH.Option::get('nonce_templet').'/');//前台模板路径
$emDispatcher = Dispatcher::getInstance();
$emDispatcher->dispatch();
View::output();

是模板显示的关键,,转到Dispatcher这个类就可以看到实现的方法

php
public static function getInstance() {//大概功能就是创建这个类
        if(self::$_instance == null) {
            self::$_instance = new Dispatcher();
            return self::$_instance;
        } else {
            return self::$_instance;
        }
    }
 
    private function __construct() {//初始化就调用的类
        $this->_path = $this->setPath();
        $this->_routingTable = Option::getRoutingTable();
 
        $urlMode = Option::get('isurlrewrite');
        foreach ($this->_routingTable as $route) {
            if (!isset($route['reg_' . $urlMode])) {
                $reg = isset($route['reg']) ? $route['reg'] : $route['reg_0'];
            } else {
                $reg = $route['reg_' . $urlMode];
            }
            if (preg_match($reg, $this->_path, $matches)) {
                $this->_model = $route['model'];
                $this->_method = $route['method'];
                $this->_params = $matches;
                break;
            } elseif (preg_match($route['reg_0'], $this->_path, $matches)) {
                $this->_model = $route['model'];
                $this->_method = $route['method'];
                $this->_params = $matches;
                break;
            }
        }
 
        if (empty($this->_model)) {
            show_404_page();
        }
    }
 
    public function dispatch(){
        $module = new $this->_model();
        $method = $this->_method;
        $module->$method($this->_params);
}

在贴上

php
$this->_routingTable = Option::getRoutingTable();

的实现方法

php
static function getRoutingTable(){
$routingtable = array(
array(
'model' => 'calendar',
'method' => 'generate',
'reg_0' => '|^.*/\?action=cal|',
),
array(
'model' => 'Log_Controller',
'method' => 'displayContent',
'reg_0' => '|^.*/\?(post)=(\d+)(&(comment-page)=(\d+))?([\?&].*)?$|',
'reg_1' => '|^.*/(post)-(\d+)\.html(/(comment-page)-(\d+))?/?([\?&].*)?$|',
'reg_2' => '|^.*/(post)/(\d+)(/(comment-page)-(\d+))?/?$|',
'reg_3' => '|^/([^\./\?=]+)(\.html)?(/(comment-page)-(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Record_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(record)=(\d{6,8})(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(record)/(\d{6,8})/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Sort_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(sort)=(\d+)(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(sort)/([^\./\?=]+)/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Tag_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(tag)=([^&]+)(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(tag)/([^/?]+)/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Author_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(author)=(\d+)(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(author)/(\d+)/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Log_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(page)=(\d+)([\?&].*)?$|',
'reg' => '|^.*/(page)/(\d+)/?([\?&].*)?$|',
),
array(
'model' => 'Search_Controller',
'method' =>'display',
'reg_0' => '|^.*/\?(keyword)=([^/&]+)(&(page)=(\d+))?([\?&].*)?$|',
),
array(
'model' => 'Comment_Controller',
'method' => 'addComment',
'reg_0' => '|^.*/\?(action)=(addcom)([\?&].*)?$|',
),
array(
'model' => 'Plugin_Controller',
'method' => 'loadPluginShow',
'reg_0' => '|^.*/\?(plugin)=([\w\-]+).*([\?&].*)?$|',
),
array(
'model' => 'Log_Controller',
'method' => 'displayContent',
'reg_0' => '|^.*?/([^/\.=\?]+)(\.html)?(/(comment-page)-(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Log_Controller',
'method' => 'display',
'reg_0' => '|^/?([\?&].*)?$|',
),
);
return $routingtable;
}

上面函数就是具体的调用函数了,'model' 是class类,'method' 是显示方式,'reg_'是通过地址的正则,想修改自定义url的修改这里就可以了。通过这种方法就直接调用了控制器,通过控制器看到首页的控制方法

前面可以理解为emlog内置的变量

后面的

include View::getView('header');

include View::getView('log_list');

就是调用的我们博客模板了。

其他都是类似

另外说下emlog源码有空行的解决办法

Emlog即使在默认的default模板上也有一行空白

虽然没什么大碍,但强迫症很可怕。。

解决办法:

在mothod.php 搜索 widget_link

然后到结尾 或者直接调到默认模板第219行

会发现后面就个空格,删除掉即可

撰写

布局切换

调整 VitePress 的布局样式,以适配不同的阅读习惯和屏幕环境。

全部展开
使侧边栏和内容区域占据整个屏幕的全部宽度。
全部展开,但侧边栏宽度可调
侧边栏宽度可调,但内容区域宽度不变,调整后的侧边栏将可以占据整个屏幕的最大宽度。
全部展开,且侧边栏和内容区域宽度均可调
侧边栏宽度可调,但内容区域宽度不变,调整后的侧边栏将可以占据整个屏幕的最大宽度。
原始宽度
原始的 VitePress 默认布局宽度

页面最大宽度

调整 VitePress 布局中页面的宽度,以适配不同的阅读习惯和屏幕环境。

调整页面最大宽度
一个可调整的滑块,用于选择和自定义页面最大宽度。

内容最大宽度

调整 VitePress 布局中内容区域的宽度,以适配不同的阅读习惯和屏幕环境。

调整内容最大宽度
一个可调整的滑块,用于选择和自定义内容最大宽度。

聚光灯

支持在正文中高亮当前鼠标悬停的行和元素,以优化阅读和专注困难的用户的阅读体验。

ON开启
开启聚光灯。
OFF关闭
关闭聚光灯。

聚光灯样式

调整聚光灯的样式。

置于底部
在当前鼠标悬停的元素下方添加一个纯色背景以突出显示当前鼠标悬停的位置。
置于侧边
在当前鼠标悬停的元素旁边添加一条固定的纯色线以突出显示当前鼠标悬停的位置。